var tableElem = "LAY-home-adPosition-list";
var tableToolsElem = "LAY-home-adPosition-tools";
var formElem = "LAY-home-adPosition-listform";
var formSubmitElem = "LAY-home-adPosition-listform-submit";

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //位置管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , url: window.globalConfig.url_adPosition_list //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'position_id', width: 100, title: 'id', fixed: 'left', sort: true}
             , {
                field: 'title', title: "页面", templet: function (d) {
                    return d.relationPage.title
                }
            }
            , {field: 'position_name', title: 'banner名称', minWidth: 100, edit: 'text'}
            // , {field: 'position_desc', title: '位置描述'}
            , {field: 'position_style', title: '尺寸建议', minWidth: 100, edit: 'text'}
            // , {field: 'position_url', title: '位置url'}
            // , {field: 'position_type', title: '位置类型'}
            , {
                field: 'adList', title: "banner数量", templet: function (d) {
                    return d.adList.length
                }
            }
            , {title: '操作', minWidth: 280, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'}
    });

    var active = {
        //添加
        add: function (othis) {
            admin.popup({
                title: '添加位置'
                , area: ['550px', '550px']
                , id: 'LAY-popup-content-add'
                , success: function (layero, index) {
                    view(this.id).render('home/adPosition/listform').done(function () {
                        form.render(null, formElem);

                        //提交
                        form.on(`submit(${formSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: window.globalConfig.url_adPosition_list,
                                type: "POST",
                                data: field,
                                success: function (res) {
                                    common.ctmMsg(res.code, res.msg)
                                    tableItem.reloadData();
                                    layer.close(index); //执行关闭
                                }
                            })
                        });
                    });
                }
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除位置
            layer.confirm('确定删除此位置？', function (index) {
                admin.req({
                    url: window.globalConfig.url_adPosition_list + "/" + data.position_id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData();
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改位置
            admin.popup({
                title: '编辑位置'
                , area: ['550px', '550px']
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {

                    setTimeout(() => {
                        form.val(formElem, {
                            page_id: data.page_id,
                        })
                    }, 500)

                    view(this.id).render('home/adPosition/listform', data).done(function () {
                        form.render(null, formElem);


                        //提交
                        form.on(`submit(${formSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            // 修改位置的请求
                            admin.req({
                                url: window.globalConfig.url_adPosition_list + "/" + field.position_id,
                                type: "PUT",
                                data: field,
                                success: function (res) {
                                    common.ctmMsg(res.code, res.msg)
                                    tableItem.reloadData();
                                    layer.close(index); //执行关闭
                                }
                            })
                        });
                    });
                }
            });
        }
    });


    // 表格文本修改
    common.tableTextEditor(tableElem, window.globalConfig.url_adPosition_list, 'position_id')
});